
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fileList:[],
    name:'',
    logo:'',
    type:"",
    id:"",
    share:"",
    merchant_member_id:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.share){
      this.setData({share:options.share})
    }
    if(options.merchant_member_id){
      this.setData({merchant_member_id:options.merchant_member_id})
    }
    this.setData({type:options.type})
    if(options.type == 'edit'){
      this.setData({id:options.id})
      this.detail()
    }
  },
  detail(){
    app._postNetWork({
      url:app.getNetAddresss("plugin.lawyer-platform.frontend.controllers.business.getDetail"),
      data:{id:this.data.id},
      success:(res)=>{
        if(res.data.result == 1){
          this.data.fileList.push({url:res.data.data.detail.logo_img_image,status:"done"})
          this.setData({name:res.data.data.detail.name,logo:res.data.data.detail.logo_img_image,fileList:this.data.fileList})
        }else{
          app.tips(res.data.msg)
        }
      },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  delete(){
    this.setData({fileList:[]})
  },
  handle() {
    if(this.data.name == ""){
      return app.tips("请输入企业名称")
    };
    if(this.data.logo==""){
      return app.tips("请上传logo")
    };
    let urlStr ="";
    let json = {
      form:{name:this.data.name,logo:this.data.logo},
    }
    if(this.data.type == 'add'){
      urlStr = app.getNetAddresss("plugin.lawyer-platform.frontend.controllers.business.create")
      if(this.data.share == 'enterprise'){
        json.form.merchant_member_id = this.data.merchant_member_id;
      }
    }else{
      json.id=this.data.id;
      urlStr = app.getNetAddresss("plugin.lawyer-platform.frontend.controllers.business.edit")
    }
    app._postNetWork({
      url:urlStr,
      data:json,
      success:(res)=>{
        if(res.data.result == 1){
          app.tips(res.data.msg)
          wx.navigateBack()
        }else{
          app.tips(res.data.msg)
        }
      },
    })
  },
  afterRead(event){
    wx.showLoading({
      title: '上传中'
    })
    const { file } = event.detail;
    let urlStr = app.getNetAddresss("upload.uploadPic");
    wx.uploadFile({
      url: urlStr,
      filePath: file.url,
      name: 'file',
      success:(resdata)=> {
        app.tips("上传成功")
        let res = JSON.parse(resdata.data);
        const { fileList = [] } = this.data;
        fileList.push({ ...file, url: res.data.img_url, status:"done", message: '上传完成'});
        this.setData({ fileList ,logo:res.data.img_url});
      },
      fail (err) {
        app.tips("上传成功")
        let fileList = [];
        fileList.push({ ...file, url: '', status:"failed", message: '上传失败'});
        this.setData({ fileList });
      },
      complete(){
        wx.hideLoading();
      }
      
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})